package com.cskaoyan.wx.service;

import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.TopicExample;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.wx.vo.TopicDetailVO;
import com.cskaoyan.wx.vo.TopicVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    TopicMapper topicMapper;

    /**
     * 话题列表
     * @param page
     * @param size
     * @return
     */
    @Override
    public TopicVo getTopicList(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        List<Topic> topics = topicMapper.selectByExample(topicExample);

        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();

        TopicVo topicVo = new TopicVo();
        topicVo.setCount(total);
        topicVo.setData(topics);

        return topicVo;
    }

    /**
     * 专题详情
     * @param id
     * @return
     */
    @Override
    public Topic getTopicRelated(Integer id) {
        Topic topic = topicMapper.selectByPrimaryKey(id);
        return topic;
    }

    /**
     * 专题详情
     * @param id
     * @return
     */
    @Override
    public TopicDetailVO getTopicDetail(Integer id) {
        Topic topic = topicMapper.selectByPrimaryKey(id);
        TopicDetailVO topicDetailVO = new TopicDetailVO();
        topicDetailVO.setGoods(topic.getGoods());
        topicDetailVO.setTopic(topic);
        return topicDetailVO;
    }
}
